package com.leehom.arch.storage.handler;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.leehom.arch.storage.exception.FileStorageException;
import com.leehom.arch.storage.exception.MessageCode;
import com.leehom.arch.storage.exception.NotADirException;
import com.leehom.arch.storage.model.FileItem;
import com.leehom.arch.storage.model.Storage;
/**
 * 
 * @类名: NativeFileStorageHandler
 * @说明: 本地存储
 *        url: file://c:/xxx/yy
 *
 * @author   leehom
 * @Date	 2018年3月13日 下午2:33:16
 * 修改记录：
 *
 * @see
 */
public class LocalFileStorageHandler extends BaseFileStorageHandler implements FileStorageHandler {

	@Override
	public String schema() {
		return "file";
	}
	
	@Override
	public InputStream getFile(String path, String fileName, Storage storage) throws FileStorageException {
		String realPath = storage.getRealPath();
		String fullPath = this.contactPath(realPath, path);
		File f = new File(fullPath, fileName);
		if(!f.exists()||f.isDirectory())
			throw new FileStorageException(MessageCode.ERROR_STORAGE_FILENOTEXISTS, fullPath);
		try {
			return new FileInputStream(f);
		} catch (FileNotFoundException e) {
			throw new FileStorageException(MessageCode.ERROR_STORAGE_FILENOTEXISTS, fullPath);
		}
	}

	@Override
	public List<FileItem> listFileOfDir(String path, Storage storage) throws FileStorageException {
		String realPath = storage.getRealPath();
		// 
		String fullPath = this.contactPath(realPath, path);
		//
		File d = new File(fullPath);
		if(!d.exists()||!d.isDirectory())
			throw new NotADirException();
		return this.toFileItems(d.listFiles(), path);
	}
	
	@Override
	public void saveFile(String path, String fileName, InputStream fileStream, Storage storage) throws FileStorageException {
		// 本地磁盘url, 需截取file后面的有效url
		// windows file://d://work/test/
		// linux file:///tmp/work/test/
		String realPath = storage.getRealPath();
		// 完整路径
		String fullPath = this.contactPath(realPath, path);
		// 获取存储文件目录
		File newFile = new File(fullPath, fileName);
		// 判断文件是否已存在
		if(newFile.exists())
			throw new FileStorageException(MessageCode.ERROR_STORAGE_FILEEXISTSED, fullPath);
		// 
		BufferedInputStream in = null;
		FileOutputStream out = null;
		try {
			this.checkOrCreateDir(newFile.getParentFile());
			//
			newFile.createNewFile();
			// 创建输入输出流
			in = new BufferedInputStream(fileStream);
			// 写入到指定的目录
			out = new FileOutputStream(newFile);
			// 设置读取字节数
			this.copyStream(in, out);
		} catch (IOException e) {
			throw new FileStorageException(MessageCode.ERROR_STORAGE_LOCALSAVEFILE, path, fileName, e.getMessage());
		} finally {
			try {
				this.closeStream(in, out);
			} catch (IOException e) {
				//
			}
		}

	}

	protected void checkOrCreateDir(File dir) throws IOException {
		if(!dir.getParentFile().exists())
			checkOrCreateDir(dir.getParentFile());
		if(!dir.exists())
			dir.mkdir();

	}
	
	protected FileItem toFileItem(File file, String path) {
		FileItem item = new FileItem();
		item.setName(file.getName());
		item.setPath(path);
		item.setSize(file.length());
		item.setDir(file.isDirectory());
		return item;
	}
	
	protected List<FileItem> toFileItems(File[] files, String path) {
		List<FileItem> vos = new ArrayList<>();
		for(File file:files) {
			vos.add(toFileItem(file, path));
		}
		return vos;
	}

	@Override
	public String getUrl(String path, String fileName, Storage storage) {
		return schema()+":"+storage.getRealPath()+path+fileName;
	}

	@Override
	public void deleteFile(String path, String fileName, Storage storage) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteDir(String path, Storage storage) {
		// TODO Auto-generated method stub
		
	}
	
}